package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.entity.WmNewsMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.media.service.IWmNewsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-08-09
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    MinIOService minIOService;
    //图片上传
    public ResponseResult<WmMaterial> upload(MultipartFile file) {
        //用户id从本地线程中获取
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //调用minio上传图片,获取url地址
        String url = minIOService.upload(file);
        //构造数据
        WmMaterial material = new WmMaterial();
        material.setUserId(user.getUserId());
        material.setUrl(url);
        material.setCreatedTime(new Date());
        material.setType(0);
        material.setIsCollection(false);
        this.save(material);
        return ResponseResult.okResult(material);
    }

    //分页查询
    public ResponseResult listByCollection(WmMaterialDto dto) {
        //获取当前用户的id
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //构造条件
        LambdaQueryWrapper<WmMaterial> qw=new LambdaQueryWrapper<>();
        //判断是否传入了收藏状态
        if (dto.getIsCollection()!=null) {
            qw.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //当前用户只能查询自己的照片
        qw.eq(WmMaterial::getUserId,user.getUserId());
        //排序，根据创建时间倒叙排序
        qw.orderByDesc(WmMaterial::getCreatedTime);
        //构造分页条件
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        //分页查询
        Page<WmMaterial> page1 = this.page(page, qw);
        //返回数据
        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }

    //收藏
    public ResponseResult collect(Integer id) {
        //获取当前用户的id
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //判断参数
        if (StringUtils.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id查询
        WmMaterial material = this.getById(id);
        material.setIsCollection(true);
        this.updateById(material);
        return ResponseResult.okResult();
    }

    //取消收藏
    public ResponseResult cancelCollect(Integer id) {
        //获取当前用户的id
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //判断参数
        if (StringUtils.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id查询
        WmMaterial material = this.getById(id);
        material.setIsCollection(false);
        this.updateById(material);
        return ResponseResult.okResult();
    }
    @Autowired
    IWmNewsMaterialService newsMaterialService;

    //删除素材
    public ResponseResult delete(Integer id) {
        //获取当前用户的id
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //判断参数
        if (StringUtils.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断素材是否被引用
        LambdaQueryWrapper<WmNewsMaterial> qw=new LambdaQueryWrapper<>();
        qw.eq(WmNewsMaterial::getMaterialId,id);
        WmNewsMaterial one = newsMaterialService.getOne(qw);
        if (one!=null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_USE);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
